/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.api.service;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.koron.workflow.api.bean.TimerBean;
import com.koron.workflow.api.service.impl.AutoCompleteServiceImpl;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
@Order(10)
public class DelayExecutor {
	
    public static ScheduledExecutorService server = Executors.newScheduledThreadPool(10);

    /**
     * 关闭延时处理执行器
     */
    public static void close() {
        try {
            server.shutdown();
            server.awaitTermination(10, TimeUnit.SECONDS);
            server.shutdownNow();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 加入任务调度
     * @param bean
     */
    public static void join(TimerBean bean) {
    	server.schedule(new AutoCompleteServiceImpl(bean), bean.getDelay(), TimeUnit.MINUTES);
    }
    
    /**
     * 加入任务调度
     * @param bean
     * @param timeUnit
     */
    public static void join(TimerBean bean, TimeUnit timeUnit) {
    	server.schedule(new AutoCompleteServiceImpl(bean), bean.getDelay(), timeUnit);
    }
    
}